package com.huijinhui.wlmtsys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.mapper.TeamsMapper;
import com.huijinhui.wlmtsys.pojo.dtos.PageDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.*;
import com.huijinhui.wlmtsys.service.FileStorageService;
import com.huijinhui.wlmtsys.service.TeamsService;
import com.huijinhui.wlmtsys.utils.BeanUtils;
import com.huijinhui.wlmtsys.utils.CollUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Slf4j
@Service
@RequiredArgsConstructor
public class TeamsServiceImpl extends ServiceImpl<TeamsMapper, WlmtsysTeams> implements TeamsService {


    private final TeamsMapper teamsMapper;


    private final FileStorageService fileStorageService;
    /**
     * 获取所有团队信息
     *
     * @return
     */
    @Override
    public List<WlmtsysTeams> getTeamsList(Integer depart) {
        List<WlmtsysTeams> teamsList = this.lambdaQuery()
                .orderByDesc(WlmtsysTeams::getCore)
                .eq(WlmtsysTeams::getDepart, depart).list();
        return teamsList;
    }

    /**
     * 保存团队信息
     *
     * @param wlmtsysTeams
     */
    @Override
    public void saveTeam(WlmtsysTeams wlmtsysTeams) {
        if (wlmtsysTeams.getDepart() != null&& wlmtsysTeams.getName() != null) {
            wlmtsysTeams.setCreateTime(LocalDateTime.now());
            this.save(wlmtsysTeams);
        }
    }

    /**
     * 删除团队信息
     *
     * @param id
     */
    @Override
    public void deleteTeam(Integer id) {
        if (id != null) {
            this.removeById(id);
        }
    }

    /**
     * 更新团队信息
     *
     * @param wlmtsysTeams
     */
    @Override
    public void updateTeam(WlmtsysTeams wlmtsysTeams) {
        wlmtsysTeams.setCreateTime(LocalDateTime.now());
        this.updateById(wlmtsysTeams);
    }

    /**
     * 分页查询团队信息
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<WlmtsysTeams> queryProjectPage(PageQuery query) {
        Page<WlmtsysTeams> page = this.lambdaQuery()
                .eq(query.getDepart()!=null,WlmtsysTeams::getDepart,query.getDepart())
                .like(query.getName()!=null,WlmtsysTeams::getName, query.getName())
                .like(query.getDesc()!=null,WlmtsysTeams::getDesc, query.getDesc())
                .page(query.toMpPage("core", false));
        List<WlmtsysTeams> records = page.getRecords();
        if (CollUtils.isEmpty(records)) {
            return PageDTO.empty(page);
        }
        List<WlmtsysTeams> wlmtsysTeamsList = new ArrayList<>();
        for (WlmtsysTeams news : records) {
            WlmtsysTeams wlmtsysTeams = BeanUtils.copyBean(news, WlmtsysTeams.class);
            wlmtsysTeamsList.add(wlmtsysTeams);
        }
        return PageDTO.of(page, wlmtsysTeamsList);
    }

    /**
     * 上传图片
     *
     * @param multipartFile
     * @param teamsId
     */
    @Override
    public ResponseResult uploadNews(MultipartFile multipartFile, Integer teamsId) {
        // 上传图片
        //1.检查参数
        if(multipartFile == null || multipartFile.getSize() == 0 || teamsId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}",fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.更新数据库
        this.lambdaUpdate().eq(WlmtsysTeams::getId,teamsId).set(WlmtsysTeams::getImage,fileId).update();
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    /**
     * 获取信息
     *
     * @param id
     * @return
     */
    @Override
    public WlmtsysTeams getTeamById(Integer id) {
        if (id != null) {
            return this.getById(id);
        }
        return null;
    }

}




